package com.example.layui.service;

import com.example.layui.mapper.MenuMapper;
import com.example.layui.pojo.Bar;
import com.example.layui.pojo.Menu;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class MenuService {

    @Resource
    MenuMapper menuMapper;
    public List<Bar> queryBars(){
        List<Bar> bars=new ArrayList<>();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object [] roles=authentication.getAuthorities().toArray();
        List<Menu> menus=new ArrayList<>();
        for (Object roleName : roles){
            List<Menu> menuList=menuMapper.queryMenusByRoleName(roleName.toString());
            menus.addAll(menuList);
        }
        Set<Menu> menuSet=new HashSet<>(menus);
        menus=new ArrayList<>(menuSet);
        for (Menu menu : menus){
            Bar bar=new Bar();
            bar.setId(menu.getId());
            bar.setMenuName(menu.getMenuName());
            bar.setParentId(menu.getParentId());
            bar.setMenu2List(menuMapper.queryMenuByParentId(menu.getId()));
            bars.add(bar);
        }
        return bars;
    }

}
